<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="skuName">
        <el-input
            v-model="queryParams.skuName"
            placeholder="请输入商品名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌" prop="brandName">
        <el-input
            v-model="queryParams.brandName"
            placeholder="请输入品牌"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格(净含量)" prop="unit">
        <el-input
            v-model="queryParams.unit"
            placeholder="请输入规格(净含量)"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品价格，单位分" prop="price">
        <el-input
            v-model="queryParams.price"
            placeholder="请输入商品价格，单位分"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品类型Id" prop="classId">
        <el-input
            v-model="queryParams.classId"
            placeholder="请输入商品类型Id"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否打折促销" prop="isDiscount">
        <el-input
            v-model="queryParams.isDiscount"
            placeholder="请输入是否打折促销"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['manage:sku:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['manage:sku:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['manage:sku:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['manage:sku:export']"
        >导出
        </el-button>
      </el-col>
      <!--  批量导入sku    -->
      <el-col :span="1.5">
        <el-upload
            :loading="uploadLoading"
            :action=uploadUrl
            :before-upload="handleBeforeUpload"
            :on-success="handleUploadSuccess"
            :on-error="handleUploadError"
            name="file"
            :show-file-list="false"
            :headers="headers"
        >
          <el-button type="primary" icon="Upload" size="mini">批量导入</el-button>
        </el-upload>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="skuList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="主键" align="center" prop="skuId"/>
      <el-table-column label="商品名称" align="center" prop="skuName"/>
      <el-table-column label="商品图片" align="center" prop="skuImage" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.skuImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="品牌" align="center" prop="brandName"/>
      <el-table-column label="规格(净含量)" align="center" prop="unit"/>
      <el-table-column label="商品价格，单位分" align="center" prop="price"/>
      <el-table-column label="商品类型Id" align="center" prop="classId"/>
      <el-table-column label="是否打折促销" align="center" prop="isDiscount"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['manage:sku:edit']">
            修改
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['manage:sku:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
    <!-- 添加或修改商品列表对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="skuRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品名称" prop="skuName">
          <el-input v-model="form.skuName" placeholder="请输入商品名称"/>
        </el-form-item>
        <el-form-item label="商品图片" prop="skuImage">
          <image-upload v-model="form.skuImage"/>
        </el-form-item>
        <el-form-item label="品牌" prop="brandName">
          <el-input v-model="form.brandName" placeholder="请输入品牌"/>
        </el-form-item>
        <el-form-item label="规格(净含量)" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入规格(净含量)"/>
        </el-form-item>
        <el-form-item label="商品价格，单位分" prop="price">
          <el-input v-model="form.price" placeholder="请输入商品价格，单位分"/>
        </el-form-item>
        <el-form-item label="商品类型Id" prop="classId">
          <el-input v-model="form.classId" placeholder="请输入商品类型Id"/>
        </el-form-item>
        <el-form-item label="是否打折促销" prop="isDiscount">
          <el-input v-model="form.isDiscount" placeholder="请输入是否打折促销"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup name="Sku">
import {
  listSku,
  getSku,
  delSku,
  addSku,
  updateSku
} from "@/api/manage/sku";
import {getToken} from "@/utils/auth.js";
const uploadUrl = ref(import.meta.env.VITE_APP_BASE_API + "/manage/sku/importData"); // 上传的图片服务器地址
const headers = ref({
  Authorization: "Bearer " + getToken()
});
const {proxy} = getCurrentInstance();
const skuList = ref([]);
const open = ref(false);
const loading = ref(true);
const uploadLoading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    skuName: null,
    skuImage: null,
    brandName: null,
    unit: null,
    price: null,
    classId: null,
    isDiscount: null,
  },
  rules: {
    skuName: [
      {
        required: true,
        message: "商品名称不能为空",
        trigger: "blur"
      }
    ],
    skuImage: [
      {
        required: true,
        message: "商品图片不能为空",
        trigger: "blur"
      }
    ],
    brandName: [
      {
        required: true,
        message: "品牌不能为空",
        trigger: "blur"
      }
    ],
    price: [
      {
        required: true,
        message: "商品价格，单位分不能为空",
        trigger: "blur"
      }
    ],
    classId: [
      {
        required: true,
        message: "商品类型Id不能为空",
        trigger: "blur"
      }
    ],
  }
});
const {
  queryParams,
  form,
  rules
} = toRefs(data);
/** 查询商品列表列表 */
function getList() {
  loading.value = true;
  listSku(queryParams.value)
      .then(response => {
        skuList.value = response.rows;
        total.value = response.total;
        loading.value = false;
      });
}
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}
// 表单重置
function reset() {
  form.value = {
    skuId: null,
    skuName: null,
    skuImage: null,
    brandName: null,
    unit: null,
    price: null,
    classId: null,
    isDiscount: null,
    createTime: null,
    updateTime: null
  };
  proxy.resetForm("skuRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.skuId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加商品列表";
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _skuId = row.skuId || ids.value
  getSku(_skuId)
      .then(response => {
        form.value = response.data;
        open.value = true;
        title.value = "修改商品列表";
      });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["skuRef"].validate(valid => {
    if (valid) {
      if (form.value.skuId != null) {
        updateSku(form.value)
            .then(response => {
              proxy.$modal.msgSuccess("修改成功");
              open.value = false;
              getList();
            });
      } else {
        addSku(form.value)
            .then(response => {
              proxy.$modal.msgSuccess("新增成功");
              open.value = false;
              getList();
            });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const _skuIds = row.skuId || ids.value;
  proxy.$modal.confirm('是否确认删除商品列表编号为"' + _skuIds + '"的数据项？')
      .then(function () {
        return delSku(_skuIds);
      })
      .then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
      })
      .catch(() => {
      });
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download('manage/sku/export',
      {
        ...queryParams.value
      },
      `sku_${new Date().getTime()}.xlsx`)
}
getList();
const handleUploadError = () => {
}
const handleUploadSuccess = (res) => {
  if (res.code === 200) {
    proxy.$modal.msgSuccess("导入成功");
  } else {
    proxy.$modal.msgError(res.msg);
  }
  uploadLoading.value = false
}
const handleBeforeUpload = () => {
  uploadLoading.value = true
}
</script>
